home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9653 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.9 KB

  1. Path: chaos.kulnet.kuleuven.ac.be!usenet
  2. From: Andreas De Troy <Andreas.DeTroy@ped.kuleuven.ac.be>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: What is referencing good for?
  5. Date: 1 Mar 1996 09:38:03 GMT
  6. Organization: KUL
  7. Message-ID: <4h6ghr$70a@chaos.kulnet.kuleuven.ac.be>
  8. NNTP-Posting-Host: pcip194.psy.kuleuven.ac.be
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 1.2N (Windows; I; 16bit)
  13.  
  14. news:313539c9.4835024@news2.cts.com
  15.  
  16. I think the ONLY reason why this referencing-thing is introduced in C++, 
  17. is to allow operator overloading.
  18.  
  19. Example: if you have a non-standard datatype and you would like to 
  20. perform the "usual" arithmetic operations on it. Say you would like to 
  21. add two complex numbers (suppose "complex" is a datatype defined by 
  22. yourself), and you don't like to write it this way:
  23.  
  24.    complex a, b, c;
  25.  
  26.    a = add (b, c);
  27.  
  28. With operator overloading, it could be written like this (don't know 
  29. whether this is perfectly legal though):
  30.  
  31.    complex tmp, b, c, *a;
  32.  
  33.    a = &tmp;
  34.  
  35.    *a = b + c;
  36.  
  37. But if you like to write it this way (the "natural" way):
  38.  
  39.    a = b + c;    // a, b, and c not integers
  40.  
  41. .. then you need to use referencing, otherwise <a> could not be modified 
  42. in a statement like that.
  43.  
  44. For the rest referencing is a rather dangerous thing and I would never 
  45. recommend using it. People who say that it allows "cleaner" code are just 
  46. missing the point. In "C" if you see something like:
  47.  
  48.    int a, b, c, d;
  49.    
  50.    swap (&a, &b);
  51.    perform_thing (c, d);
  52.  
  53. .. you know that a and b can be changed inside the function, and c and d 
  54. not. This is very helpful for debugging. In C++ you don't know this, and 
  55. you actually have to look at the exact function-definition, somewhere in 
  56. a header, to make sure that c and d can be modified or not.
  57.  
  58. So I would NEVER use the &-operator to produce so-called "cleaner" code.
  59.  
  60.  
  61.